package List;

import java.util.Arrays;

public class MyArrayList implements Ilist {
    private int[] elem;
    private int size;
    public static final int ELEM_SIZE = 10;

    public MyArrayList() {
        this.elem = new int[ELEM_SIZE];
    }

    public MyArrayList(int MySize) {
        this.elem = new int[MySize];
    }

    private void initcapacity() {
        if (full()) {
            elem = Arrays.copyOf(elem, elem.length * 2);
        }
    }

    private boolean full() {
        return this.size == elem.length;
    }

    @Override
    public void add(int data) {
        initcapacity();
        elem[this.size++] = data;
    }

    private boolean cheekpos(int pos) throws PosException {
        if (pos < 0 || pos > size) {
            throw new PosException("下标不合理");
        }
        return false;
    }

    @Override
    public void add(int pos, int data) throws PosException {
        cheekpos(pos);
        initcapacity();
        for (int i = size - 1; i >= pos; i--) {
            elem[size] = elem[i];
        }
        elem[pos] = data;
        size++;
    }

    private boolean empty() throws MyArrayListEmpty {
        if (this.size == 0) {
            throw new MyArrayListEmpty("数组为空");
        }
        return false;
    }

    // 判定是否包含某个元素
    @Override
    public boolean contains(int toFind) throws MyArrayListEmpty {
        empty();
        for (int i = 0; i < this.size; i++) {
            if (elem[i] == toFind) {
                return true;
            }
        }
        return false;
    }

    // 查找某个元素对应的位置
    @Override
    public int indexOf(int toFind) throws MyArrayListEmpty {
        empty();
        for (int i = 0; i < size; i++) {
            if (elem[i] == toFind) {
                return i;
            }
        }
        return -1;
    }

    private boolean cheekgetpos(int pos) throws PosException {
        if (pos < 0 || pos >= size) {
            throw new PosException("下标不合理");
        }
        return true;
    }

    @Override
    public int get(int pos) throws PosException, MyArrayListEmpty {
        empty();
        cheekgetpos(pos);
        return elem[pos];
    }

    @Override
    public void set(int pos, int value) throws PosException, MyArrayListEmpty {
        empty();
        cheekgetpos(pos);
        elem[pos] = value;
    }

    //删除第一次出现的关键字key
    @Override
    public void remove(int toRemove) throws MyArrayListEmpty {
        int key = indexOf(toRemove);
        if (key == -1) {
            System.out.println("没有这个数字！");
            return;
        }
        for (int i = key; i < size - 1; i++) {
            elem[i] = elem[i + 1];
        }
        size--;
    }

    @Override
    public int size() {
        return size;
    }

    @Override
    public void clear() {
        Arrays.fill(elem, 0);
        this.size=0;
    }

    @Override
    public void display() {
        for (int i = 0; i < size; i++) {
            System.out.println(elem[i] + " ");
        }
        System.out.println();
    }
}
